package com.sz.app.oss.api.common.customer;

import com.sz.biz.app.web.BaseController;
import com.sz.biz.common.constants.MessagesCodes;
import com.sz.biz.logistics.base.service.AirportService;
import com.sz.biz.logistics.base.service.PortService;
import com.sz.biz.common.customer.service.CustomerUserAddressService;
import com.sz.biz.common.customer.dto.CusUserAddressDto;
import com.sz.biz.logistics.constants.BaseConstants;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户地址 <br>
 * Author: qiankun <br>
 * Date: 2017-05-12 09:52:51
 */
@Controller
@RequestMapping("${api}/customer/useraddress")
@Api(description = " ", tags = "037-006、客户信息管理-客户地址")
public class CusUserAddressController extends BaseController {

    @Autowired
    private CustomerUserAddressService customerUserAddressService;
    @Autowired
    private AirportService airportService;
    @Autowired
    private PortService portService;

    @ApiOperation(value = "查询地址", notes = "查询地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "地址ID", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerId", value = "客户id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "地址类型", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "transportType", value = "全球海运:1,全球速递:11,全球空运:6", required = false, dataType = "int", paramType = "query")})
    @RequestMapping(value = "/search", method = {RequestMethod.GET})
    @ResponseBody
    public ResultDto findUserAddress(@RequestParam(required = false) Integer id,
                                     @RequestParam(required = false, defaultValue = "0") Integer customerId, @RequestParam(required = false) Integer type,
                                     @RequestParam(required = false) Integer transportType) {
        ResultDto result = new ResultDto();
        List<CusUserAddressDto> lists = customerUserAddressService.queryByCusIdAndIdsAndType(id, customerId, type, transportType);
        result.setData(lists);
        return result;
    }

    @ApiOperation(value = "保存或者修改", notes = "保存修改<br>权限：")
    @RequestMapping(value = "/saveOrUpdate", method = {RequestMethod.POST})
    @ResponseBody
    public ResultDto saveOrUpdateUserAddress(@RequestBody @Validated CusUserAddressDto addressDto) {
        int result = customerUserAddressService.saveOrUpdate(addressDto);
        ResultDto dto = null;
        if (addressDto.getId() != null && addressDto.getId() > 0) {
            dto = getOperateSuccessDto(MessagesCodes.MODULE_CUSTOMER_ADDRESS_UPDATE_SUCCESS);
        } else {
            dto = getOperateSuccessDto(MessagesCodes.MODULE_CUSTOMER_ADDRESS_ADD_SUCCESS);
        }
        dto.setData(result);
        return dto;
    }

    @ApiOperation(value = "删除地址", notes = "删除地址：")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ids", required = false, dataType = "List", paramType = "body"),
            @ApiImplicitParam(name = "cusUserId", value = "cusUserId", required = true, dataType = "int", paramType = "path")})
    @RequestMapping(value = "/delete/{cusUserId}", method = {RequestMethod.POST})
    @ResponseBody
    public ResultDto deleteByUserIdAndAddressId(@RequestBody(required = false) List<Integer> ids,
                                                @PathVariable(required = false) int cusUserId) {
        customerUserAddressService.delete(ids, cusUserId);
        ResultDto resultDto = this
                .getOperateSuccessDto(MessagesCodes.MODULE_CUSTOMER_ADDRESS_DELETE_SUCCESS);
        return resultDto;
    }

    @ApiOperation(value = "设置默认地址", notes = "设置默认地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oldId", value = "oldId", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "newId", value = "newId", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerId", value = "customerId", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "地址类型", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "transportType", value = "全球海运:1,全球速递:11,全球空运:6", required = true, dataType = "int", paramType = "query")})
    @RequestMapping(value = "/setdefault", method = {RequestMethod.POST})
    @ResponseBody
    public ResultDto setDefault(@RequestParam(required = false) Integer oldId,
                                @RequestParam(required = false, defaultValue = "0") Integer newId,
                                @RequestParam(required = false, defaultValue = "0") Integer customerId,
                                @RequestParam(required = false, defaultValue = "0") Integer type,
                                @RequestParam(required = false, defaultValue = "0") Integer transportType) {
        customerUserAddressService.setDefault(oldId, newId, customerId, type, transportType);
        ResultDto resultDto = this
                .getOperateSuccessDto(MessagesCodes.MODULE_CUSTOMER_ADDRESS_SET_DEFAULT_ADDRESS_SUCCESS);
        resultDto.setData(newId);
        return resultDto;
    }

    @ApiOperation(value = "查询是否已经达到客户地址数据", notes = "查询是否已经达到客户地址数量")
    @RequestMapping(value = "/query/addresscount/{customerId}", method = {RequestMethod.GET})
    @ResponseBody
    public ResultDto queryAddressCount(@PathVariable Integer customerId, @RequestParam(required = false) Integer type, @RequestParam(required = false) Integer transportType) {
        ResultDto resultDto = new ResultDto();
        int count = customerUserAddressService.queryCountByCustomerIdAndType(customerId, type, transportType);
        resultDto.setData(count);
        return resultDto;
    }
    
    /**
     * 分页查询机场或港口
     *
     * @throws Exception
     */
    @ApiOperation(value = "查询机场或港", notes = "查询机场或港<br>权限：")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "查询的关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型：1为港口，6为机场", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000", required = true, dataType = "int", paramType = "query")
           })
    @RequestMapping(value = "/shortlist", method = {RequestMethod.GET})
    public
    @ResponseBody
    ResultDto findShortList(@RequestParam(required = false) String q,@RequestParam(required = true) String type,
    		@RequestParam(required = false, defaultValue = "0") int pageIndex, @RequestParam(required = false, defaultValue = "10") int pageSize) {
    	ResultDto dto =null;
    	q = StringUtils.replaceSqlPattern(q);
        if(BaseConstants.TransportType.AIR.equals(type)){
        	dto = airportService.shortAirportListPage(new QueryParams(pageSize, pageIndex, q, null, false));
        }
        else if(BaseConstants.TransportType.PORT.equals(type)){
        	dto = portService.shortPortListPage(new QueryParams(pageSize, pageIndex, q, null, false));
        }
        return dto;
    }

}
